package com.parking.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.util.DateUtil;
import com.parking.common.web.util.RedisUtil;
import com.parking.order.dao.PlatformIncomeDao;
import com.parking.order.model.CentifiedIncome;
import com.parking.order.model.RentersIncome;
import com.parking.order.dao.RentersIncomeDao;
import com.parking.order.service.IRentersIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.order.util.BasicInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@Service
public class RentersIncomeServiceImpl extends ServiceImpl<RentersIncomeDao, RentersIncome> implements IRentersIncomeService {
    /**
     * Redis工具类
     * */
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 出租客收入数据层
     * */
    @Autowired
    private RentersIncomeDao rentersIncomeDao;
    /**
     * 其他模块调用工具类
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;
    /**
     * 出租客查询12月每日收入
     * 参数 出租客Id rentersId
     * 返回 List<Integer> 1-31日 收入 BigDecimal->int
     * */
    @Override
    public List<Integer> findDailyIncomeRenters(Integer rentersId) throws Exception {
        List<Integer> incomes = new ArrayList<>();
        Integer rentersInfoId = basicInfoUtil.getRenters(rentersId).getRentersInfoId();

        for(int i=1;i<=31;i++){
            // 返回12月1日的收入 BigDecimal->Int
            String day = "2023-12-" + (i<10?"0"+i:i);
            RentersIncome renters = rentersIncomeDao.selectOne(new QueryWrapper<RentersIncome>().eq("year_month_time",day).eq("renters_info_id",rentersInfoId));
            int income = renters==null?0:renters.getIncome().intValue();
            if(DateUtil.localDateToString(LocalDate.now()).equals(day)){
                // 当天还要算Redis
                if(redisUtil.isZSetMember(RedisKey.rentersIncome(), String.valueOf(rentersInfoId))){
                    income += (int) redisUtil.ZSetScore(RedisKey.rentersIncome(), String.valueOf(rentersInfoId));
                }
            }
            incomes.add(income);
        }

        return incomes;
    }
}
